package com.example.kehoobackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.kehoobackend.common.CommentType;
import com.example.kehoobackend.common.CommonResponse;
import com.example.kehoobackend.common.FollowType;
import com.example.kehoobackend.common.LikeType;
import com.example.kehoobackend.domain.*;
import com.example.kehoobackend.persistance.AnswerMapper;
import com.example.kehoobackend.persistance.LikeMapper;
import com.example.kehoobackend.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service("answerService")
public class AnswerServiceImpl extends ServiceImpl<AnswerMapper, Answer> implements AnswerService {


//    @Autowired
//    QueryWrapper<Answer> queryWrapper;
//
//    @Autowired
//    QueryWrapper<Like> likeQueryWrapper;
//
//    @Autowired
//    QueryWrapper<Comment> commentQueryWrapper;

    @Autowired
    LikeMapper likeMapper;

    @Autowired
    UserService userService;

    @Autowired
    FollowService followService;

    @Autowired
    CommentService commentService;

    @Autowired
    CollectionService collectionService;

    @Lazy
    @Autowired
    DayInfoService dayInfoService;



    @Override
    public CommonResponse<List<Answer>> getAnswerByQuestionId(Integer question_id,Integer user_id) {
        QueryWrapper<Answer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("question_id",question_id);
        List<Answer> answers = list(queryWrapper);
        for(Answer answer : answers){
            boolean is_follow = followService.isFollow(user_id,answer.getUser_id(), FollowType.USER);
            answer.setIs_follow(is_follow);
            boolean is_like = isLike(answer.getAnswer_id(),user_id);
            answer.setIs_like(is_like);

            User user = userService.getById(answer.getUser_id());

            String user_name = user.getUsername();
            answer.setUser_name(user_name);
            String user_job = user.getIndustry();
            answer.setUser_job(user_job);
            Integer comment_count;
            comment_count = getCommentCount(answer.getAnswer_id());
            answer.setComment_count(comment_count);
        }

        setIsCollected(answers,user_id);
        return CommonResponse.createForSuccess("查询问题所有回答",answers);
    }

    @Override
    public CommonResponse<List<Answer>> getAnswerByUserId(Integer user_id) {
        QueryWrapper<Answer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user_id);
        List<Answer> answers = list(queryWrapper);
        return CommonResponse.createForSuccess("查询用户所有回答",answers);
    }

    @Override
    public Answer addAnswer(Answer answer) {
        if(save(answer)){
            Integer answer_id = answer.getAnswer_id();
            Answer newAnswer = getById(answer_id);

            //今日新增回答数加1
            String currentDate = String.valueOf(LocalDate.now());
            DayInfo dayInfo = dayInfoService.getById(currentDate);
            dayInfo.setNew_answers_count(dayInfo.getNew_answers_count()+1);
            dayInfoService.updateById(dayInfo);

            return newAnswer;
        }else return null;
    }

    @Override
    public boolean deleteAnswer(Integer answer_id) {
         removeById(answer_id);

         //删除回答下的所有评论
        QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
        commentQueryWrapper.eq("to_id",answer_id).eq("type",CommentType.ANSWER).select("comment_id");
        List<Comment> comments = commentService.list(commentQueryWrapper);
        for(Comment comment : comments){
            commentService.removeById(comment);
        }

        return true;

    }

    @Override
    public boolean like(Integer answer_id, Integer user_id) {
        //存储点赞关系
        Answer answer = getById(answer_id);
        answer.setLiked_count(answer.getLiked_count()+1);
        if(updateById(answer)){
            Like like = new Like();
            like.setUser_id(user_id);
            like.setTarget_id(answer_id);
            like.setType(LikeType.ANSWER);

            //TODO:确定返回值是多少
            likeMapper.insert(like);
            return true;
        }else return false;
    }

    @Override
    public boolean cancelLike(Integer answer_id, Integer user_id) {
        QueryWrapper<Like> likeQueryWrapper = new QueryWrapper<>();
        Answer answer = getById(answer_id);
        answer.setLiked_count(answer.getLiked_count()-1);
        if(updateById(answer)){
            likeQueryWrapper.eq("user_id",user_id).eq("target_id",answer.getAnswer_id())
                    .eq("type",LikeType.ANSWER);
            return likeMapper.delete(likeQueryWrapper) == 1;
        }else return false;
    }

    @Override
    public boolean isLike(Integer answer_id, Integer user_id) {
        QueryWrapper<Like> likeQueryWrapper = new QueryWrapper<>();
        likeQueryWrapper.eq("user_id",user_id)
                .eq("target_id",answer_id).eq("type", LikeType.ANSWER);
        Like like = likeMapper.selectOne(likeQueryWrapper);
        return like != null;
    }

    @Override
    public Integer getCommentCount(Integer answer_id) {
        QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
        commentQueryWrapper.eq("to_id",answer_id)
                .eq("type", CommentType.ANSWER);
        return (int)commentService.count(commentQueryWrapper);
    }

    @Override
    public CommonResponse<List<Answer>> getAllAnswer() {
        return CommonResponse.createForSuccess("查询所有问题",list());
    }

    public void setIsCollected(List<Answer> answers,Integer user_id){
        for(Answer answer : answers){
            answer.setIs_collected(collectionService.isCollected(answer.getAnswer_id(),user_id));
        }
    }
}
